package com.scalar.chiptrack.sales.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.InfoMessages;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Labels;
import com.scalar.chiptrack.sales.dao.NewCustomerDAO;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.MessageResources;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;


public class CustomerSalesPersonRelationshipAction extends ChipTrackAction{

    private static final String ACTION_NAME = "/customerSalesPersonRelationship.do";


    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    						HttpServletResponse response) throws Exception {

    	HttpSession session = request.getSession();
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);

        if(user == null)
        {
	    	session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
	    	return ( mapping.findForward ( "login" ) );
        }
        String action = request.getParameter("action");
        String customerId =  request.getParameter("customerId") ;
        String odmId = request.getParameter("odmId") ;

        if("saveSalesPersons".equalsIgnoreCase( action ) )
        {
            return saveAllSalesPersonsList(mapping, form, request, response);
        }
        else if( customerId != null )
        {
            return getAllSalesPersonsList(mapping, form, request, response);
        }

        return mapping.findForward("searchSalesPersons");
 }
   public ActionForward getAllSalesPersonsList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{

       String customerId = request.getParameter("customerId");
       String odmId = request.getParameter("odmId");
       int custId = 0;
       int odmcustId = 0;

       try{
            custId = Integer.parseInt( customerId );
        }
        catch (NumberFormatException ignore){
        }
        try{
            odmcustId = Integer.parseInt( odmId );
        }
        catch (NumberFormatException ignore){
        }


    	ArrayList salesPersonsList = NewCustomerDAO.getAllSalesPersonsList( custId  ) ;
        if( odmcustId > 0 )       //if Odm is selected for the sales order load ODM sales persons
        {
          ArrayList odmSalesPersonsList = NewCustomerDAO.getAllSalesPersonsList( odmcustId ) ;
          request.setAttribute("odmSalesPersonsList", odmSalesPersonsList);
          request.setAttribute("odmName", CustomerDAO.getCustomerName( null, odmcustId ) );
        }

    	request.setAttribute("salesPersonsList", salesPersonsList);
    	request.setAttribute("customerName", CustomerDAO.getCustomerName( null, custId  ) );
    	request.setAttribute("customerId", customerId);

    	return mapping.findForward("searchSalesPersons");
    }

    public ActionForward saveAllSalesPersonsList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{

    	String customerId = request.getParameter("customerId");
        String odmId = request.getParameter("odmId");
        int custId = 0;
        int odmcustId = 0;

        try{
            custId = Integer.parseInt( customerId );
        }
        catch (NumberFormatException ignore){
        }
        try{
            odmcustId = Integer.parseInt( odmId );
        }
        catch (NumberFormatException ignore){
        }

    	HashMap info = new HashMap();
    	HashMap errors = new HashMap();

    	String salesPersonId[] = request.getParameterValues("salesPersonId");
        String endCustomerLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.endCustomer" );

    	if( salesPersonId == null )
        {

    		if( NewCustomerDAO.deactivateSalesPersonList( customerId ) ) {

    			info.put(InfoMessages.CUSTOMER_RELATIONSHIP_UPDATE_SALESPERSON, null);
    		}

    		ArrayList salesPersonsList = NewCustomerDAO.getAllSalesPersonsList(  custId  );

            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
	    	request.setAttribute("salesPersonsList", salesPersonsList);
	    	request.setAttribute("customerName", CustomerDAO.getCustomerName( null, custId ) );
	    	request.setAttribute("customerId", customerId);

    	}
        else
        {

            if( NewCustomerDAO.deactivateSalesPersonList( customerId ) ) {

                    //info.put(InfoMessages.CUSTOMER_RELATIONSHIP_UPDATE_SALESPERSON, null);
            }

            if( NewCustomerDAO.saveSalesPersonList( customerId, salesPersonId ) ) {

                request.setAttribute("closeWindow", "true");

                info.put(InfoMessages.CUSTOMER_RELATIONSHIP_SAVE_SALESPERSON, null);

            }

            ArrayList salesPersonsList = NewCustomerDAO.getAllSalesPersonsList( custId );
            request.setAttribute("salesPersonsList", salesPersonsList);
	    	request.setAttribute("customerName", CustomerDAO.getCustomerName( null, custId ) );
	    	//request.setAttribute("customerId", customerId);
        }
        //If ODM is seleted for the Sales Order loading the ODM sales Person Details.
        if(  odmcustId > 0 )
        {
            String odmSalesPersonId[] = request.getParameterValues("odmSalesPersonId");

            if( odmSalesPersonId == null )
            {

                if( NewCustomerDAO.deactivateSalesPersonList( odmId ) ) {

                    info.put( endCustomerLabel+" and Sales Person Relation Updated successfully", null);
                }

                ArrayList odmSalesPersonsList = NewCustomerDAO.getAllSalesPersonsList( custId );

                request.setAttribute("odmSalesPersonsList", odmSalesPersonsList);
                request.setAttribute("odmName", CustomerDAO.getCustomerName( null,  odmcustId ) );
                //request.setAttribute("odmId", odmId);

    	    }
            else
            {

                if( NewCustomerDAO.deactivateSalesPersonList( odmId ) ) {

                        //info.put(InfoMessages.CUSTOMER_RELATIONSHIP_UPDATE_SALESPERSON, null);
                }

                if( NewCustomerDAO.saveSalesPersonList( odmId, odmSalesPersonId ) ) {

                    request.setAttribute("closeWindow", "true");

                    info.put( endCustomerLabel+" and Sales Person Relation Saved successfully", null);

                }

                ArrayList odmSalesPersonsList = NewCustomerDAO.getAllSalesPersonsList( odmcustId  );
                request.setAttribute("odmSalesPersonsList", odmSalesPersonsList);
                request.setAttribute("odmName", CustomerDAO.getCustomerName( null, odmcustId ) );
                //request.setAttribute("odmId", odmId);
            }
        }

    	request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
    	request.setAttribute ( SessionKeys.INFO_MESSAGES, info );

    	return mapping.findForward("searchSalesPersons");
    }
}